import {accAdd, accMultiply} from "../utils/math";

class Calculator {
    totalPrice = 0
    totalSkuCount = 0
    cartItems = []

    constructor(cartItems) {
        this.cartItems = cartItems
        this._init(cartItems)
    }

    _init(cartItems) {
        cartItems.forEach(item=>{
            this._push(item)
        })
    }

    _push(cartItem) {
        const price = cartItem.sku.discount_price ? cartItem.sku.discount_price : cartItem.sku.price
        const partTotalPrice = accMultiply(cartItem.count, price)
        this.totalPrice = accAdd(this.totalPrice, partTotalPrice)
        this.totalSkuCount += cartItem.count
    }

}

export {
    Calculator
}
